package com.yangwp.doutu.controller;

import com.yangwp.doutu.Utils.MapUtil;
import com.yangwp.doutu.enums.RespEnum;
import com.yangwp.doutu.service.DoutuUserService;
import com.yangwp.doutu.service.ImageService;
import com.yangwp.doutu.service.LikeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/like")
public class LikeController {

    @Autowired
    private ImageService imageService;
    @Autowired
    private DoutuUserService userService;
    @Autowired
    private LikeService likeService;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(String userId, String img){
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(img)){
            return MapUtil.getMap(RespEnum.MISSING_PARAM);
        }
        if(!userService.isUserExists(userId) || !imageService.isImageExists(img)){
            return MapUtil.getMap(RespEnum.PARAM_ERROR);
        }

        if(likeService.isLikeExists(userId, img)){
            return MapUtil.getMap(RespEnum.OK);
        }

        if(likeService.add(userId, img)){
            return MapUtil.getMap(RespEnum.OK);
        }
        return MapUtil.getMap(RespEnum.SERVER_FAIL);
    }
}
